package com.enjoy.game.xcard.sprite;

import org.andengine.entity.IEntity;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.TiledSprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.font.FontUtils;
import org.andengine.opengl.font.IFont;
import org.andengine.opengl.shader.ShaderProgram;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.DrawType;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.HorizontalAlign;

import com.enjoy.game.xcard.sprite.CardInfo.DamageDirection;

import android.graphics.Typeface;

public class XCard extends TiledSprite implements IExEntity {
	public static final int CARD_WIDTH = 120;
	public static final int CARD_HEIGHT = 160;

	public static final int CARD_TEXTURE_WIDTH = CARD_WIDTH * 2 * 2;
	public static final int CARD_TEXTURE_HEIGHT = CARD_HEIGHT * 2;
	
	public static final int FRONT_INDEX = 0;
	public static final int BACK_INDEX = 1;
	
	public static final int DC_PADDING = 2;

	boolean mGrabbed = false;
	CardInfo mCardInfo;
	OnClickListener mOnClickListener;
	
	private final Text[] mDCText = new Text[DamageDirection.values().length];

	public XCard(float pX, float pY, ITiledTextureRegion pTiledTextureRegion,
			VertexBufferObjectManager pVertexBufferObjectManager, IFont font,  CardInfo cardInfo) {
		super(pX, pY, CARD_WIDTH, CARD_HEIGHT, pTiledTextureRegion, pVertexBufferObjectManager);
		mCardInfo = cardInfo;
		float x, y;
		x = getWidth()/2;
		y = 0;
		mDCText[DamageDirection.TOP.ordinal()] = new Text(x, y, font, Integer.toString(mCardInfo.getTopDC()),
				new TextOptions(HorizontalAlign.CENTER), pVertexBufferObjectManager);
		String rightDC = Integer.toString(mCardInfo.getRightDC());
		x =  getWidth() - FontUtils.measureText(font, rightDC) - DC_PADDING;
		y = getHeight()/2 - font.getLineHeight()/2; 
		mDCText[DamageDirection.RIGHT.ordinal()] = new Text(x, y , font, Integer.toString(mCardInfo.getRightDC()),
				new TextOptions(HorizontalAlign.RIGHT), pVertexBufferObjectManager);
		
		x = getWidth()/2;
		y = getHeight() - font.getLineHeight(); 
		mDCText[DamageDirection.BOTTOM.ordinal()] = new Text(x, y , font, Integer.toString(mCardInfo.getBottomDC()),
				new TextOptions(HorizontalAlign.CENTER), pVertexBufferObjectManager);
		
		x = 0;
		y = getHeight()/2 - font.getLineHeight()/2;  
		mDCText[DamageDirection.LEFT.ordinal()] = new Text(x, y , font, Integer.toString(mCardInfo.getLeftDC()),
				new TextOptions(HorizontalAlign.LEFT), pVertexBufferObjectManager);
		
		for(DamageDirection direction : DamageDirection.values()){
			attachChild(mDCText[direction.ordinal()]);
		}
		
	}

	public void show(boolean back) {

		if (back && getCurrentTileIndex() != BACK_INDEX) {
			setCurrentTileIndex(BACK_INDEX);
		} else if (!back && getCurrentTileIndex() != FRONT_INDEX) {
			setCurrentTileIndex(FRONT_INDEX);
		}
	}
	
	

	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
			float pTouchAreaLocalX, float pTouchAreaLocalY) {
		switch (pSceneTouchEvent.getAction()) {
		case TouchEvent.ACTION_DOWN:
			this.mGrabbed = true;
			break;
		case TouchEvent.ACTION_MOVE:
			break;
			
		case TouchEvent.ACTION_UP:
			if (this.mGrabbed) {
				this.mGrabbed = false;
				if (mOnClickListener != null) {
					mOnClickListener.onClick(this);
				}
			}
			break;
		}
		return true;
	}

	@Override
	public void onAttachScene(Scene scene) {
		//ExEntityUtil.onAttachScene(scene, this);
	}

	@Override
	public boolean canAcceptDrag() {
		return ExEntityUtil.canAcceptDrag(this);
	}

	@Override
	public boolean dispatchDragEvent(EntityDragEvent event) {
		return ExEntityUtil.dispatchDragEvent(event, this);
	}

	public void setOnClickListener(OnClickListener l) {
		mOnClickListener = l;
	}
}
